package com.xgggh.redpack.base.ui;


import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.google.android.material.snackbar.Snackbar;

/**
 * Created by az on 2018/5/24.
 */

public  class  RxBaseActivity extends AppCompatActivity {
    protected final long windowDuration = 2; //2s

    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void setSupportActionBar(@Nullable Toolbar toolbar) {
        super.setSupportActionBar(toolbar);
        toolBarSet(toolbar);
    }

    public void setSupportActionBarJust(@Nullable Toolbar toolbar) {
        super.setSupportActionBar(toolbar);
    }

    protected void toolBarSet(@Nullable Toolbar toolbar){
        if (getSupportActionBar()==null){
            return;
        }
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setTitle(null);
        //默认显示android:label
        TextView title= findViewById(android.R.id.title);
        if (title!=null){
            title.setText(getTitle());
        }
    }

    /**
     * 权限申请
     * @param permission 权限名称
     * @param requestCode 请求码
     * @return 是否申请成功
     */
    int code=0;
    protected boolean mayRequestPermissions(String[] permission,int requestCode) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return true;
        }
        for (String p: permission) {
            if (ContextCompat.checkSelfPermission(this,p) == PackageManager.PERMISSION_GRANTED) {
                return true;
            }
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,p)) {
                    Snackbar.make(getWindow().getDecorView(),"",Snackbar.LENGTH_LONG)
                        .setText("请打开权限")
                        .setAction("确定", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                code=1;
                            }
                        })
                        .show();
                break;
            } else {
                code=1;
                break;
            }
        }
        if (code==0){
            return true;
        }
        if (code==1){
            ActivityCompat.requestPermissions(this,
                    permission,
                    requestCode);
        }
        return false;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case android.R.id.home:
                finish();
                return true;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }


}
